/**
 * Created by webrx on 2017-09-29.
 */
$(function () {
    $(".findPwd-input").click(function () {
        $(this).children(".findPwd-ts").hide();
        $(this).children("input").focus()
    });
    $(".nreg_input,.nreg_sinput").focus(function () {
        $(this).addClass("nreg_yellowbor").siblings(".findPwd-ts").hide()
    }).focusout(function () {
        $(this).removeClass("nreg_yellowbor");
        $(this).val() == "" ? $(this).siblings(".findPwd-ts").show() : $(this).siblings(".findPwd-ts").hide()
    });
    try {
        YGM.Util.changeValidateImage2()
    } catch (a) {
    }
});

function checkUserEmailIsExistValidate(b) {
    var a = false;
    if (b == "") {
        $("#email_msg").attr("class", "findPwd-tips mt10").html("请输入账号");
        return a
    }
    $.ajax({
        type: "POST", url: basePath + "/my/checkMemberLoginAccount.jhtml?memberName=" + b, async: false, success: function (c) {
            if (c != "success") {
                $("#email_msg").attr("class", "findPwd-tips mt10").html(c);
                return a
            } else {
                a = true
            }
        }
    });
    return a
}

function changeValidateImage() {
    $("#code_").val("");
    var a = "https:" == document.location.protocol ? "https://passport.yougou.com" : "http://www.yougou.com";
    $("#imageValidate").attr("src", a + "/servlet/imageCaptcha?rand=" + Math.random())
}

function validateCode_lega() {
    if ($("#code_").val().length < 4) {
        return false
    }
    var a = false;
    $.ajax({
        type: "POST", url: basePath + "/api/checkCode2.sc", data: {code: $("#code_").val()}, dataType: "json", async: false, success: function (b) {
            if (parseInt(b) == 1) {
                a = true
            }
        }
    });
    return a
}

function validateForm() {
    $("#code_msg").attr("class", "findPwd-tips mt8").html("");
    $("#email_msg").attr("class", "findPwd-tips mt10").html("");
    var b = false;
    var c = $("#email_").val();
    var a = $("#code_").val();
    if (!checkUserEmailIsExistValidate(c)) {
        if (c != "") {
            $("#email_msg").attr("class", "errortips findPwd-tips mt10").html("对不起，该账户不存在");
            return b
        }
    }
    if (!validateCode_lega()) {
        $("#code_msg").attr("class", "errortips findPwd-tips mt8").html("验证码错误");
        changeValidateImage();
        return b
    }
    $("#memberName").val(c);
    return true
}

function validCode() {
    var b = $("#code2_").val();
    if (b == "") {
        YGM.Util.show("codeTips2", "验证码不能为空");
        return false
    }
    var a = false;
    $.ajax({
        type: "POST", async: false, url: "/api/checkCode2.sc", data: "code=" + b, success: function (c) {
            if (parseInt(c) == 0) {
                msg = "验证码不正确";
                YGM.Util.changeValidateImage2();
                YGM.Util.show("codeTips2", msg);
                a = false
            } else {
                a = true;
                YGM.Util.clear()
            }
        }
    });
    return a
}

function checkradio() {
    var b = $('input:radio[name="findPwd-style"]:checked').val();
    if (b == "email") {
        var a = checkEmailLimit();
        if (!a) {
            document.frm.action = "/toReActiveEmail.jhtml";
            setTimeout(function () {
                document.frm.submit()
            }, 500)
        } else {
            return
        }
    } else {
        if (b == "phone") {
            document.frm.action = "/toReActivePhone.jhtml";
            setTimeout(function () {
                document.frm.submit()
            }, 500)
        } else {
            alert("请选择找回密码的方式");
            return
        }
    }
}

function checkEmailLimit() {
    var a = false;
    $.ajax({
        type: "POST", async: false, url: "/usercenter/memberUpdatePwd/checkEmailLimit.sc", success: function (b) {
            if (b != "true") {
                alert(b);
                a = true
            }
        }
    });
    return a
}

function sendEmail() {
    var a = null;
    $.ajax({
        type: "POST", async: false, url: "/my/toSendEmail.jhtml", success: function (b) {
            if (b != "true") {
                alert(b);
                a = "1"
            }
        }
    });
    if (a == "1") {
        return false
    }
}

function sendCodeBtn() {
    var validResult = validCode();
    if (!validResult) {
        return
    }
    $("#sendTips").attr("class", "findPwd-tips mt2").html("");
    $("#sendCodeBtn").attr("disabled", "disabled");
    $("#sendCodeBtn").attr("disabled", "disabled");
    $("#sendTips").attr("class", "righttips findPwd-tips mt2").html("验证码已发送，30分钟内有效");
    var time1 = 59;
    timer1 = setInterval(function () {
        $("#sendCodeBtn").val(time1 + "秒后可重新获取");
        time1 -= 1;
        if (time1 <= 0) {
            $("#sendCodeBtn").val("重新获取验证码").attr("disabled", "");
            clearInterval(timer1)
        }
    }, 1000);
    $.ajax({
        type: "POST", data: "validCode=" + $("#code2_").val(), url: "/my/getPhoneCode.jhtml", success: function (data) {
            var info = eval("(" + data + ")");
            if (data == null) {
                $("#sendTips").attr("class", "errortips findPwd-tips mt2").html("获取验证码失败!")
            } else {
                if (info.code == "S_OK") {
                    $("#sendTips").attr("class", "righttips findPwd-tips mt2").html("验证码已发送，30分钟内有效")
                } else {
                    if (info.code == "S_FAIL") {
                        $("#sendTips").attr("class", "errortips findPwd-tips mt2").html("获取验证码失败!")
                    } else {
                        if (info.code == "E_ERR") {
                            $("#sendTips").attr("class", "errortips findPwd-tips mt2").html("手机号码存在异常，请更换手机号码!")
                        } else {
                            if (info.code == "P_ERR") {
                                $("#sendTips").attr("class", "errortips findPwd-tips mt2").html("手机号码格式不正确!")
                            } else {
                                if (info.code == "V_ERR") {
                                    $("#sendTips").attr("class", "errortips findPwd-tips mt2").html("提示：1分钟内不可重复获取验证码!")
                                } else {
                                    if (info.code == "validCodeFail") {
                                        $("#sendTips").attr("class", "errortips findPwd-tips mt2").html("提示：验证码校验失败!")
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    })
}

function checkCode() {
    $("#code_msg").attr("class", "findPwd-tips mt8").html("");
    $("#codeTips").show();
    if ($("#code").val() == "") {
        $("#code").focus();
        $("#code_msg").attr("class", "errortips findPwd-tips mt8").html("验证码不能为空");
        return false
    }
    var type = false;
    $.ajax({
        type: "POST", async: false, url: "/my/checkPhoneCode.jhtml?code=" + $("#code").val(), success: function (data) {
            var info = eval("(" + data + ")");
            if (info.code == "V_FAIL") {
                $("#code").focus();
                $("#code_msg").attr("class", "errortips findPwd-tips mt8").html("验证码错误")
            } else {
                if (info.code == "F_FAIL") {
                    $("#code").focus();
                    $("#code_msg").attr("class", "errortips findPwd-tips mt8").html("验证码无效，请重新获取验证码")
                } else {
                    if (info.code == "V_OK") {
                        $("#code_msg").hide();
                        type = true
                    }
                }
            }
        }
    });
    if (type) {
        return true
    } else {
        return false
    }
}

function checksubmit() {
    if (!checkCode()) {
        return
    }
    document.phonefrm.submit()
}

function checkCodes(b) {
    $("#code_msg").attr("class", "findPwd-tips mt8").html("");
    $("#pwd1").removeClass("righttips").removeClass("errortips").html("");
    $("#pass_msg").removeClass("righttips").removeClass("errortips").html("");
    if ($("#findPwd_password").val().length < 6) {
        $("#pwd1").removeClass("righttips").addClass("errortips").html("密码应6-25位之间");
        return false
    }
    if ($("#findPwd_confirmPassword").val().length > 25) {
        $("#pwd1").removeClass("righttips").addClass("errortips").html("密码应6-25位之间");
        return false
    }
    if ($("#findPwd_confirmPassword").val() != $("#findPwd_password").val()) {
        $("#pass_msg").removeClass("righttips").addClass("errortips").html("两次密码输入不一致");
        return false
    }
    if (b != "1") {
        if ($("#code").val() == "") {
            $("#code").focus();
            $("#code_msg").attr("class", "errortips findPwd-tips mt8").html("验证码不能为空");
            return false
        }
        var a = false;
        $.ajax({
            type: "POST", async: false, dataType: "json", url: "/my/checkCode.jhtml?code=" + $("#code").val() + "&sign=" + $("#sign").val() + "&t=" + Math.random(), success: function (c) {
                if (c.state != "success") {
                    $("#code_msg").attr("class", "errortips findPwd-tips mt8").html(c.msg)
                } else {
                    a = true
                }
            }
        });
        if (!a) {
            return false
        }
    }
    document.form2.submit()
}

$(function () {
    var a = $("#email_").val();
    if (a) {
        $(".findPwd-ts").hide()
    }
});